package chat_api

import (
	"github.com/gin-gonic/gin"
	"github.com/liu-cn/json-filter/filter"
	"giv-vue-blog/models"
	"giv-vue-blog/models/res"
	"giv-vue-blog/service/common"
)

// ChatListView 获取聊天记录列表
// @Summary 获取聊天记录列表
// @Tags 群聊管理
// @Produce json
// @Param page query int false "页码，默认为1"
// @Param limit query int false "每页数量，默认为10"
// @Param sort query string false "排序字段，默认为created_at desc"
// @Router /api/chat_records_list [get]
func (ChatApi) ChatListView(c *gin.Context) {
	var cr models.PageInfo
	err := c.ShouldBindQuery(&cr)
	if err != nil {
		res.FailedWithCode(res.ArgumentError, c)
		return
	}
	cr.Sort = "created_at desc"
	list, count, _ := common.CommonList(models.ChatModel{IsGroup: true}, common.Option{
		PageInfo: cr,
	})
	//解决不显示字段的空值问题
	data := filter.Omit("list", list)
	_list, _ := data.(filter.Filter)
	if string(_list.MustMarshalJSON()) == "{}" {
		list = make([]models.ChatModel, 0)
		res.OkWithList(list, count, c)
		return
	}
	res.OkWithList(data, count, c)
}
